package com.gla.market.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gla.market.R;
import com.gla.market.activity.Shop_FYHActivity;
import com.gla.market.entity.ShopCart;
import com.gla.market.entity.ShopInCart;

import java.util.List;

/**
 * 购物车店铺Adapter
 */
public class ShopListAdapter extends RecyclerView.Adapter<ShopListAdapter.ViewHolder>{

    private List<ShopInCart> shopBeanList;
    private Context context;

    public ShopListAdapter(Context context) {
        this.context = context;
    }

    public void setshopBeanList(List<ShopInCart> shopBeanList) {
        this.shopBeanList = shopBeanList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shopcart_shop, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopInCart shop = shopBeanList.get(position);
        if (shop.getChoosed()) {
            holder.ckOneChose.setChecked(true);
        } else {
            holder.ckOneChose.setChecked(false);
        }
        holder.tv_shopName.setText(shop.getName());
        holder.tv_shopName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Shop_FYHActivity.class);
                intent.putExtra("id",String.valueOf(shop.getId()));
                context.startActivity(intent);
            }
        });
        //设置商品list
        ShoppingCartAdapter adapter =  new ShoppingCartAdapter(context);
        adapter.setShoppingCartBeanList(shop.getList());
        holder.commodityList.setLayoutManager(new LinearLayoutManager(context));
        holder.commodityList.setAdapter(adapter);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,dip2px(context,60+shop.getList().size() * 110));
        holder.root.setLayoutParams(params);

        //店铺单选框按钮
        holder.ckOneChose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shop.setChoosed(((CheckBox) v).isChecked());
                        List<ShopCart> list = shop.getList();
                        //店铺所有商品被选择
                        for(int i=0;i<list.size();i++){
                            list.get(i).setChoosed(shop.getChoosed());
                            Intent intent = new Intent("com.gla.market.CHECK");
                            intent.putExtra("shopcart_id",list.get(i).getId());
                            intent.putExtra("checked",((CheckBox) v).isChecked());
                            context.sendBroadcast(intent);
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
        );
    }

    @Override
    public int getItemCount() {
        return shopBeanList == null ? 0 : shopBeanList.size();
    }

    //初始化控件
    class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout root;
        CheckBox ckOneChose;
        TextView tv_shopName;
        RecyclerView commodityList;
        public ViewHolder(View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.shopcart_shop_root);
            ckOneChose = itemView.findViewById(R.id.ck_shopcart_chose);
            tv_shopName = itemView.findViewById(R.id.shopcart_shop_name);
            commodityList = itemView.findViewById(R.id.shopcart_shop_content);
        }
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
